package com.ztesoft.huangfu.learning.springboot.springbootaliyun.controller;


import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.ztesoft.huangfu.learning.springboot.springbootaliyun.utils.HttpUtils.doPost;

@RestController
public class AliyunController {


    @Value("${aliweather}")
    String aliweather ; //阿里云天气url

    @GetMapping("/testApi")
    public String testApi() {
        return "testApi ;from 8999 ";
    }

    @PostMapping("/weatherApi")
    public String weatherApi(HttpServletRequest httpServletRequest,@RequestParam Map<String, String> bodys) {

        //https://apigateway.console.aliyun.com/inner/?spm=5176.730006-56956004-57096001-cmapi012362/A.content.8.AGUVWE#/cn-beijing/apps/testApi/075be4991b0946d4b850e22d9b175504/47fc729dc8c5420daa8f6761766d64c1/RELEASE/24996463/CloudMarket
        //guoyao:token:d01246ac6284b5a591f875173e9e2a18
        //guoyao:appcode:f5aaa6d9aa824b7fb4ce4a4b893bbca0

        //wode:token:d01246ac6284b5a591f875173e9e2a18
        //appcode:b325630199374ac98f4ab65346d03012


        httpServletRequest.getHeader("Authorization");

        String str = "";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", httpServletRequest.getHeader("Authorization"));
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        try {
            HttpResponse response = doPost(aliweather, null, null, headers, null, bodys);

            /**读取服务器返回过来的json字符串数据**/
            str = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}
